package models

import (
	"order_srv/basic/config"
	"time"
)

// Billing 账单表
type Billing struct {
	ID           uint32    `gorm:"column:id;type:int UNSIGNED;primaryKey;autoIncrement;not null;" json:"id"`
	BillSn       string    `gorm:"column:bill_sn;type:varchar(50);comment:账单编号;not null;unique;" json:"bill_sn"`
	UserID       uint32    `gorm:"column:user_id;type:int UNSIGNED;comment:用户ID;not null;" json:"user_id"`
	UserType     uint8     `gorm:"column:user_type;type:tinyint UNSIGNED;comment:用户类型 1-乘客 2-司机;not null;" json:"user_type"`
	BillType     uint8     `gorm:"column:bill_type;type:tinyint UNSIGNED;comment:账单类型 1-日账单 2-周账单 3-月账单;not null;" json:"bill_type"`
	BillPeriod   string    `gorm:"column:bill_period;type:varchar(20);comment:账单周期;not null;" json:"bill_period"` // 如：2024-01-01
	TotalIncome  float64   `gorm:"column:total_income;type:decimal(10,2);comment:总收入;not null;default:0;" json:"total_income"`
	TotalExpense float64   `gorm:"column:total_expense;type:decimal(10,2);comment:总支出;not null;default:0;" json:"total_expense"`
	NetAmount    float64   `gorm:"column:net_amount;type:decimal(10,2);comment:净收入;not null;default:0;" json:"net_amount"`
	Status       uint8     `gorm:"column:status;type:tinyint UNSIGNED;comment:状态 1-待确认 2-已确认 3-有争议;not null;default:1;" json:"status"`
	GeneratedAt  time.Time `gorm:"column:generated_at;type:datetime;comment:生成时间;not null;" json:"generated_at"`
	ConfirmedAt  time.Time `gorm:"column:confirmed_at;type:datetime;comment:确认时间;default:NULL;" json:"confirmed_at"`
	CreateAt     time.Time `gorm:"column:create_at;type:datetime;not null;default:CURRENT_TIMESTAMP;" json:"create_at"`
	UpdateAt     time.Time `gorm:"column:update_at;type:datetime;not null;default:CURRENT_TIMESTAMP;" json:"update_at"`
	DeleteAt     time.Time `gorm:"column:delete_at;type:datetime;default:NULL;" json:"delete_at"`
	Remark       string    `gorm:"column:remark;type:varchar(500);comment:备注;default:'';" json:"remark"`
}

func (b *Billing) TableName() string {
	return "billing"
}

// Create 创建账单
func (b *Billing) Create() error {
	return config.DB.Create(b).Error
}

// Update 更新账单
func (b *Billing) Update() error {
	return config.DB.Save(b).Error
}

// GetByID 根据ID获取账单
func (b *Billing) GetByID(id uint32) error {
	return config.DB.Where("id = ?", id).First(b).Error
}

// GetByBillSn 根据账单编号获取账单
func (b *Billing) GetByBillSn(billSn string) error {
	return config.DB.Where("bill_sn = ?", billSn).First(b).Error
}

// BillingDetail 账单明细表
type BillingDetail struct {
	ID              uint32    `gorm:"column:id;type:int UNSIGNED;primaryKey;autoIncrement;not null;" json:"id"`
	BillingID       uint32    `gorm:"column:billing_id;type:int UNSIGNED;comment:账单ID;not null;" json:"billing_id"`
	OrderID         uint32    `gorm:"column:order_id;type:int UNSIGNED;comment:订单ID;not null;" json:"order_id"`
	OrderSn         string    `gorm:"column:order_sn;type:varchar(50);comment:订单编号;not null;" json:"order_sn"`
	ItemType        uint8     `gorm:"column:item_type;type:tinyint UNSIGNED;comment:项目类型 1-收入 2-支出;not null;" json:"item_type"`
	ItemName        string    `gorm:"column:item_name;type:varchar(100);comment:项目名称;not null;" json:"item_name"`
	Amount          float64   `gorm:"column:amount;type:decimal(10,2);comment:金额;not null;" json:"amount"`
	Description     string    `gorm:"column:description;type:varchar(500);comment:描述;default:'';" json:"description"`
	TransactionTime time.Time `gorm:"column:transaction_time;type:datetime;comment:交易时间;not null;" json:"transaction_time"`
	CreateAt        time.Time `gorm:"column:create_at;type:datetime;not null;default:CURRENT_TIMESTAMP;" json:"create_at"`
	UpdateAt        time.Time `gorm:"column:update_at;type:datetime;not null;default:CURRENT_TIMESTAMP;" json:"update_at"`
}

func (bd *BillingDetail) TableName() string {
	return "billing_detail"
}

// Create 创建账单明细
func (bd *BillingDetail) Create() error {
	return config.DB.Create(bd).Error
}

// BatchCreate 批量创建账单明细
func (bd *BillingDetail) BatchCreate(details []BillingDetail) error {
	return config.DB.CreateInBatches(details, 100).Error
}

// GetByBillingID 根据账单ID获取明细
func (bd *BillingDetail) GetByBillingID(billingID uint32) ([]BillingDetail, error) {
	var details []BillingDetail
	err := config.DB.Where("billing_id = ?", billingID).Find(&details).Error
	return details, err
}

// AbnormalTransaction 异常交易表
type AbnormalTransaction struct {
	ID             uint32    `gorm:"column:id;type:int UNSIGNED;primaryKey;autoIncrement;not null;" json:"id"`
	OrderID        uint32    `gorm:"column:order_id;type:int UNSIGNED;comment:订单ID;not null;" json:"order_id"`
	OrderSn        string    `gorm:"column:order_sn;type:varchar(50);comment:订单编号;not null;" json:"order_sn"`
	UserID         uint32    `gorm:"column:user_id;type:int UNSIGNED;comment:用户ID;not null;" json:"user_id"`
	AbnormalType   uint8     `gorm:"column:abnormal_type;type:tinyint UNSIGNED;comment:异常类型 1-重复扣款 2-金额不符 3-时间异常 4-状态异常;not null;" json:"abnormal_type"`
	AbnormalDesc   string    `gorm:"column:abnormal_desc;type:varchar(500);comment:异常描述;not null;" json:"abnormal_desc"`
	ExpectedAmount float64   `gorm:"column:expected_amount;type:decimal(10,2);comment:预期金额;not null;" json:"expected_amount"`
	ActualAmount   float64   `gorm:"column:actual_amount;type:decimal(10,2);comment:实际金额;not null;" json:"actual_amount"`
	Status         uint8     `gorm:"column:status;type:tinyint UNSIGNED;comment:处理状态 1-待处理 2-已处理 3-已忽略;not null;default:1;" json:"status"`
	ProcessResult  string    `gorm:"column:process_result;type:varchar(500);comment:处理结果;default:'';" json:"process_result"`
	ProcessedAt    time.Time `gorm:"column:processed_at;type:datetime;comment:处理时间;default:NULL;" json:"processed_at"`
	ProcessedBy    uint32    `gorm:"column:processed_by;type:int UNSIGNED;comment:处理人ID;default:NULL;" json:"processed_by"`
	CreateAt       time.Time `gorm:"column:create_at;type:datetime;not null;default:CURRENT_TIMESTAMP;" json:"create_at"`
	UpdateAt       time.Time `gorm:"column:update_at;type:datetime;not null;default:CURRENT_TIMESTAMP;" json:"update_at"`
}

func (at *AbnormalTransaction) TableName() string {
	return "abnormal_transaction"
}

// Create 创建异常交易记录
func (at *AbnormalTransaction) Create() error {
	return config.DB.Create(at).Error
}

// Update 更新异常交易记录
func (at *AbnormalTransaction) Update() error {
	return config.DB.Save(at).Error
}

// GetByStatus 根据状态获取异常交易
func (at *AbnormalTransaction) GetByStatus(status uint8) ([]AbnormalTransaction, error) {
	var transactions []AbnormalTransaction
	err := config.DB.Where("status = ?", status).Find(&transactions).Error
	return transactions, err
}

// DisputeRecord 争议记录表
type DisputeRecord struct {
	ID          uint32    `gorm:"column:id;type:int UNSIGNED;primaryKey;autoIncrement;not null;" json:"id"`
	BillingID   uint32    `gorm:"column:billing_id;type:int UNSIGNED;comment:账单ID;not null;" json:"billing_id"`
	UserID      uint32    `gorm:"column:user_id;type:int UNSIGNED;comment:用户ID;not null;" json:"user_id"`
	DisputeType uint8     `gorm:"column:dispute_type;type:tinyint UNSIGNED;comment:争议类型 1-金额不符 2-重复扣费 3-服务未提供 4-其他;not null;" json:"dispute_type"`
	DisputeDesc string    `gorm:"column:dispute_desc;type:text;comment:争议描述;not null;" json:"dispute_desc"`
	Evidence    string    `gorm:"column:evidence;type:varchar(50);comment:证据材料;default:'';" json:"evidence"`
	Status      uint8     `gorm:"column:status;type:tinyint UNSIGNED;comment:状态 1-待处理 2-处理中 3-已解决 4-已关闭;not null;default:1;" json:"status"`
	Resolution  string    `gorm:"column:resolution;type:varchar(50);comment:解决方案;default:'';" json:"resolution"`
	ResolvedAt  time.Time `gorm:"column:resolved_at;type:datetime;comment:解决时间;default:NULL;" json:"resolved_at"`
	ResolvedBy  uint32    `gorm:"column:resolved_by;type:int UNSIGNED;comment:解决人ID;default:NULL;" json:"resolved_by"`
	CreateAt    time.Time `gorm:"column:create_at;type:datetime;not null;default:CURRENT_TIMESTAMP;" json:"create_at"`
	UpdateAt    time.Time `gorm:"column:update_at;type:datetime;not null;default:CURRENT_TIMESTAMP;" json:"update_at"`
}

func (dr *DisputeRecord) TableName() string {
	return "dispute_record"
}

// Create 创建争议记录
func (dr *DisputeRecord) Create() error {
	return config.DB.Create(dr).Error
}

// Update 更新争议记录
func (dr *DisputeRecord) Update() error {
	return config.DB.Save(dr).Error
}

// GetByStatus 根据状态获取争议记录
func (dr *DisputeRecord) GetByStatus(status uint8) ([]DisputeRecord, error) {
	var disputes []DisputeRecord
	err := config.DB.Where("status = ?", status).Find(&disputes).Error
	return disputes, err
}

// GetByUserID 根据用户ID获取争议记录
func (dr *DisputeRecord) GetByUserID(userID uint32) ([]DisputeRecord, error) {
	var disputes []DisputeRecord
	err := config.DB.Where("user_id = ?", userID).Find(&disputes).Error
	return disputes, err
}
